<?php

    require '../twl-php/autoload.php';


    Output::htmlHeader();
    //ob_start(create_function('$str', 'return convert_cyr_string($str, "w", "d");'));

//    for ($r = 0; $r < 255; $r+= 16) {
//        for ($g = 0; $g < 255; $g+= 16) {
//            for ($b = 0; $b < 255; $b+= 1) {
//                echo "<b style='background-color:" . Color::createFromRGBBytes($r, $g, $b)->getHtmlCode() . "'>. </b>";
//            }
//        }
//    }

    foreach (Dir::getFiles('typo', '*.dat') as $file) {
        echo preform_tests(create_function('$str', 'return Typo::getInstance()->processHtml($str);'), $file);
    }

//echo typo(file_get_contents('pens.html'));

function preform_tests($func, $test_file) {

    $rez = '';

    $execute_test = true;
    $n_errors = 0;
    $n_tests  = 0;

    foreach (file($test_file) as $line_number => $str) {

        $str = trim($str);
        if (!$str) continue;

        $str = strtr($str, array(
            '\r' => "\r",
            '\n' => "\n",
        ));

        if ($execute_test) {

            $in  = $str;
            $out = call_user_func($func, $in);
            $n_tests++;

        } else {

            $cfg = $str;

            if ($out != $cfg) {

                $cfg = strtr($cfg, array(
                    '&nbsp;' => '•',
                    '<' => '&lt;',
                    '>' => '&gt;',
                ));

                $out = strtr($out, array(
                    '&nbsp;' => '•',
                    '<' => '&lt;',
                    '>' => '&gt;',
                ));

                //var_dump($cfg);
                //echo "<br>";
                //var_dump($out);
                //echo "<br>";

                $rez .= "<div>Error at line: ".++$line_number."</div>\n";

                $rez .= "<pre>";
                $rez .= " IN: $in\n";
                $rez .= "OUT: $out\n";
                $rez .= "CFG: $cfg\n";
                $rez .= "</pre><hr>";
                $n_errors++;
            }
        }

        $execute_test = !$execute_test;
    }

    if (!$execute_test) $rez .= "WARNING! [$test_file] is corrupted!\n";

    $rez = "<h1>$test_file: </h1>
        <div>errors: $n_errors  / $n_tests</div><br>
        $rez";

    return $rez;
}